package it.polimi.TravelDream.ejb.searchManager;

import it.polimi.TravelDream.ejb.dtos.AddressDTO;
import it.polimi.TravelDream.ejb.entities.Address;
import it.polimi.TravelDream.ejb.userManager.AddressSearch;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class AddressManager
 */
@Stateless
public class AddressManager implements AddressSearch {
	
	@PersistenceContext
    private EntityManager em;
	
	private List <Address> result = new ArrayList <Address>();

    /**
     * Default constructor. 
     */
    public AddressManager() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public List <AddressDTO> getAllAddressesList(){
    	List <AddressDTO> addressDTOs = new ArrayList <AddressDTO>();
    	this.result = em.createNamedQuery(Address.FIND_ALL, Address.class).getResultList();
    	
    	for (Address a:this.result){
    		AddressDTO addressDTO = convertToDTO(a);
    		addressDTOs.add(addressDTO);
    	}
    	return addressDTOs;
    }
        
    private AddressDTO convertToDTO (Address a){
    	AddressDTO AddressDTO = new AddressDTO();
    	AddressDTO.setStreet(a.getStreet());
    	AddressDTO.setStreetNumber(a.getStreetNumber());
    	AddressDTO.setCity(a.getCity());
    	AddressDTO.setCap(a.getCap());
    	AddressDTO.setDistrict(a.getDistrict());
    	AddressDTO.setNation(a.getNation());
    	return AddressDTO;
    }

}
